import java.util.LinkedList;
import java.util.Queue;

public class demo1 {
    int []dx=new int[]{0,0,1,-1};
    int []dy=new int[]{1,-1,0,0};
    public int[][] floodFill(int[][] image, int sr, int sc, int color) {
        int ret=image[sr][sc];
        int m=image.length;
        int n=image[0].length;
        if(image[sr][sc]==color)return image;
        Queue<int []> queue=new LinkedList();
        queue.add(new int[]{sr,sc});
        while (!queue.isEmpty()){
            int arr[]=queue.poll();
            int x=arr[0];
            int y=arr[1];
            image[x][y]=color;
            for(int i=0;i<4;i++){
                x+=dx[i];
                y+=dy[i];
                if(x<m&&x>=0&&y>=0&&y<n&&image[x][y]!=color){
                    queue.add(new int[]{x,y});
                }
            }
        }
        return image;
    }
}
